<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增广告弹窗</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<style>
    .layui-form-label{
        width: 60px !important;
    }
</style>
<body style="height: 750px;background-color: white">
<div style="width:80%;margin:15px auto;text-align: center" class="layui-btn-group" id="btngroup">
    <button type="button" id="btnchina" class="layui-btn layui-btn-primary" onclick="clickbtn(0)">中文</button>
    <button type="button" id="btnenglish" class="layui-btn layui-btn-primary" onclick="clickbtn(1)">英语</button>
    <!--    泰语-->
    <button type="button" id="btnlaowo" class="layui-btn " onclick="clickbtn(2)">泰语</button>
    <button type="button" id="btnjianpuzhai" class="layui-btn layui-btn-primary" onclick="clickbtn(3)">柬埔寨</button>
</div>
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;">
    <form class="layui-form" action="">
        <!-- 图片上传 -->
        <div class="layui-upload" id="zh_div" style="display: none">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;" >上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" src="/image/no.jpg" name="logo" id="logo" style="width: 40%">
                <p id="demoText"></p>
            </div>
<!--            <span style="font-size: 14px;color: red;margin-left: 110px" >建议图片的高宽比为 1 : 1.8，例如200*360 </span>-->
            <input type="hidden" class="input-text" id="picture" name="picture">
        </div>
        <!-- 图片上传 -->
        <div class="layui-upload" id="en_div" style="display: none">
            <button type="button" class="layui-btn" id="test2" style="display: inline;float: left;" >上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" src="/image/no.jpg" name="logo2" id="logo2" style="width: 40%">
                <p id="demoText2"></p>
            </div>
            <!--            <span style="font-size: 14px;color: red;margin-left: 110px" >建议图片的高宽比为 1 : 1.8，例如200*360 </span>-->
            <input type="hidden" class="input-text" id="picture2" name="picture2">
        </div>
        <!-- 图片上传 -->
        <div class="layui-upload" id="kh_div" style="display: none">
            <button type="button" class="layui-btn" id="test4" style="display: inline;float: left;" >上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" src="/image/no.jpg" name="logo4" id="logo4" style="width: 40%">
                <p id="demoText4"></p>
            </div>
            <!--            <span style="font-size: 14px;color: red;margin-left: 110px" >建议图片的高宽比为 1 : 1.8，例如200*360 </span>-->
            <input type="hidden" class="input-text" id="picture4" name="picture4">
        </div>
        <!-- 图片上传 -->
        <div class="layui-upload" id="tai_div">
            <button type="button" class="layui-btn" id="test3" style="display: inline;float: left;" >上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" src="/image/no.jpg" name="logo3" id="logo3" style="width: 40%;">
                <p id="demoText3"></p>
            </div>
            <!--            <span style="font-size: 14px;color: red;margin-left: 110px" >建议图片的高宽比为 1 : 1.8，例如200*360 </span>-->
            <input type="hidden" class="input-text" id="picture3" name="picture3">
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" >状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span >启用</span></div>
                <input type="radio" name="status" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span >禁用</span></div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye" >立即添加</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary" >重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    var $ = layui.jquery;

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var upload = layui.upload;

        //添加操作
        form.on('submit(addWuye)',function(data){
            var bg = $("input[name='picture']").val();
            var picture_en = $("input[name='picture2']").val();
            var picture_tai = $("input[name='picture3']").val();
            var picture_kh = $("input[name='picture4']").val();

            if(bg==null || bg==""){
                layer.msg('请上传中文图片！');
            }else if(picture_en ==null || picture_tai == ""){
                layer.msg('请上传英文图片！');
            }else if (picture_tai == null || picture_tai == ""){
                layer.msg("请上传泰文图片");
            }else if (picture_kh == null || picture_kh == ""){
                layer.msg("请上传柬埔寨图片");
            }
            else{
                data.field.picture=bg;
                data.field.picture_en=picture_en;
                data.field.picture_tai=picture_tai;
                data.field.picture_kh=picture_kh;
                var msgMovie = layer.load(1);
                $.ajax({
                    url:"/advertise/addAdvertise"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==200){
                            layer.close(layer.index);
                            layer.msg('添加成功');
                            setTimeout(function(){
                                window.location.reload();
                            },1000);
                        }else {
                            layer.msg(data.msg);
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
                layer.close(msgMovie);
            }
            return false;
        });

        //bg上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/admin/upload/uploadimg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        //bg上传
        var uploadInst2 = upload.render({
            elem: '#test2'
            , url: '/admin/upload/uploadimg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo2').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture2").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst2.upload();
                });
            }
        });

        //bg上传
        var uploadInst3 = upload.render({
            elem: '#test3'
            , url: '/admin/upload/uploadimg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo3').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture3").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst3.upload();
                });
            }
        });

        //bg上传
        var uploadInst4 = upload.render({
            elem: '#test4'
            , url: '/admin/upload/uploadimg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo4').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture4").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst4.upload();
                });
            }
        });

        // 设置下拉列表的高度
        $(".layui-form-select dl").css("max-height",'400px');
    });

    function clickbtn(index) {
        yuyan=index;
        var btns=$("#btngroup").find("button");
        if(index==0){
            $("#zh_div").css("display","block");
            $("#en_div").css("display","none");
            $("#tai_div").css("display","none");
            $("#kh_div").css("display","none");
        } else if(index==1){
            $("#zh_div").css("display","none");
            $("#en_div").css("display","block");
            $("#tai_div").css("display","none");
            $("#kh_div").css("display","none");
        }else if(index==2){
            $("#zh_div").css("display","none");
            $("#en_div").css("display","none");
            $("#tai_div").css("display","block");
            $("#kh_div").css("display","none");
        }else if (index==3){
            $("#zh_div").css("display","none");
            $("#en_div").css("display","none");
            $("#tai_div").css("display","none");
            $("#kh_div").css("display","block");
        }
        for(let i=0;i<btns.length;i++){
            if(i==index){
                btns[i].setAttribute("class","layui-btn");
                continue;
            }
            btns[i].setAttribute("class","layui-btn layui-btn-primary");
        }
    }
</script>

</body>
</html>